package socketserver;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

import log.Logger;


public class SocketServer extends net.Server {
	private static final long serialVersionUID = 1L;
	public static final int PORT = 32000;
		
	
	public String getName() {
		return "SocketServer";
	}
	
	
	public String getDescription() {
		return "TCP server";
	}
	
	
	public void run() {
		try {
			this.socket = new ServerSocket(SocketServer.PORT);
			Logger.outputMessage("SocketServer up and running! (Port: " + SocketServer.PORT + ")");
		} catch (IOException e) {
			Logger.outputMessage("Error binding server to port " + SocketServer.PORT + "!");
			return;
		}

		while (true) {
			try {
				Socket clientSocket = this.socket.accept();

				SocketClientHandler handler = new SocketClientHandler(
						new SocketClientCommunicator(clientSocket));
				new Thread(handler).start();
			} catch (IOException e) {
				Logger.outputMessage("IOException waiting for client connection on " + SocketServer.class + "!");
				e.printStackTrace();
			}
		}
	}
	
	
	private ServerSocket socket;
}
